`
https://leetcode.cn/problems/frequency-of-the-most-frequent-element/
`

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var maxFrequency = function (nums, k) {
  // 思路：https://leetcode.cn/problems/frequency-of-the-most-frequent-element/solutions/742562/pai-xu-qian-zhui-he-er-fen-zuo-duan-dian-xegv/
  const n = nums.length
  nums.sort((a, b) => a - b)
  let sum = 0
  let cost = 0
  let res = 0
  let left = 0, right = 0

  while (right < n) {
    const n1 = nums[right++]
    sum += n1
    cost = (right - left) * n1 - sum

    while (cost > k) {
      const n2 = nums[left++]
      sum -= n2
      cost = (right - left) * n1 - sum
    }

    res = Math.max(res, right - left)
  }

  return res
};